package com.seckill.controller.user;

import com.seckill.pojo.dto.ActivityPageQueryDTO;
import com.seckill.service.SeckillActivityService;
import com.seckill.service.SeckillCouponService;
import com.seckill.service.SeckillProductService;

import com.sky.result.PageResult;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户端秒杀接口
 */
@RestController
@RequestMapping("/user/seckill")
@Api(tags = "用户端秒杀接口")
@Slf4j
public class SeckillController {

    @Autowired
    private SeckillActivityService seckillActivityService;

    @Autowired
    private SeckillCouponService seckillCouponService;

    @Autowired
    private SeckillProductService seckillProductService;

    /**
     * 查询正在进行中的秒杀活动
     * @return
     */
    @GetMapping("/activity/page")
    @Operation(summary = "dada",description = "dasf")
    public Result<PageResult> activityPage(@RequestBody ActivityPageQueryDTO activityPageQueryDTO) {
        log.info("查询正在进行中的秒杀活动：第{}页，每页{}条",activityPageQueryDTO.getPage(),activityPageQueryDTO.getPageSize());
        PageResult pageResult = seckillActivityService.pageQuery(activityPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据活动id查询秒杀优惠券
     * @param page
     * @param pageSize
     * @param activityId
     * @return
     */
    @GetMapping("/coupon/page")
    @ApiOperation("根据活动id查询秒杀优惠券")
    public Result<PageResult> couponPage(int page, int pageSize, Long activityId) {
        log.info("根据活动id查询秒杀优惠券：第{}页，每页{}条，活动ID：{}", page, pageSize, activityId);
        PageResult pageResult = seckillCouponService.pageQuery(page, pageSize, activityId);
        return Result.success(pageResult);
    }

    /**
     * 根据活动id查询秒杀特价农产品
     * @param page
     * @param pageSize
     * @param activityId
     * @return
     */
    @GetMapping("/product/page")
    //@ApiOperation("根据活动id查询秒杀特价农产品")
    public Result<PageResult> productPage(int page, int pageSize, Long activityId) {
        log.info("根据活动id查询秒杀特价农产品：第{}页，每页{}条，活动ID：{}", page, pageSize, activityId);
        PageResult pageResult = seckillProductService.pageQuery(page, pageSize, activityId);
        return Result.success(pageResult);
    }

    /**
     * 领取优惠券
     * @param couponId
     * @return
     */
    @PostMapping("/coupon/{couponId}")
    @ApiOperation("领取优惠券")
    public Result<String> takeCoupon(@PathVariable Long couponId) {
        log.info("领取优惠券：{}", couponId);
        // 从ThreadLocal中获取当前登录用户的id
        Long userId = 1L; // 这里应该从ThreadLocal中获取，暂时写死
        seckillCouponService.take(couponId, userId);
        return Result.success();
    }

    /**
     * 秒杀下单
     * @param productId
     * @param count
     * @return
     */
    @PostMapping("/product/{productId}/{count}")
    @ApiOperation("秒杀下单")
    public Result<Long> seckillOrder(@PathVariable Long productId, @PathVariable Integer count) {
        log.info("秒杀下单：商品ID：{}，数量：{}", productId, count);
        // 从ThreadLocal中获取当前登录用户的id
        Long userId = 1L; // 这里应该从ThreadLocal中获取，暂时写死
        Long orderId = seckillProductService.seckillOrder(productId, userId, count);
        return Result.success(orderId);
    }
}